<?php
/**
 * @category   Application_Extensions
 * @package    Talktangle Plugin 
 * @copyright  Idragon Co., Ltd.
 * @license
 */
class Talktangle_Widget_MypromotiontangleController extends Engine_Content_Widget_Abstract {
		
	public function indexAction() {
	    
        $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer();
        $coreApi = Engine_Api::_()->talktangle();
        
        $talkT = Engine_Api::_()->getDbtable('talktangles','talktangle');
        
        $db = $talkT->getAdapter();
        $union = new Zend_Db_Select($db);
        
        
        $select = $talkT->select()->from($talkT->info('name'),array('promotion_id')) ->where('winner_id = ?',$viewer->user_id);
        $union->union(array('('.$select->__toString().')'));
        
        $crossT = Engine_Api::_()->getDbtable('crosstangles','talktangle');
        $select = $crossT->select()->from($crossT->info('name'),array('promotion_id')) ->where('winner_id = ?',$viewer->user_id);
        $union->union(array('('.$select->__toString().')'));
        
        $picT = Engine_Api::_()->getDbtable('pictangles','talktangle');
        $select = $picT->select()->from($picT->info('name'),array('promotion_id')) ->where('winner_id = ?',$viewer->user_id);
        $union->union(array('('.$select->__toString().')'));
        
        $promotion_ids = $db->fetchAll($union);
        
        if( empty($promotion_ids) )
        {
			$this->view->paginator = null; 
			return;
          return $this->setNoRender();
        }

        // Process ids
        $ids = array();
        foreach( $promotion_ids as $data )
        {
          $ids[] = $data['promotion_id'];
        }
        $ids = array_unique($ids);
    
        $promotionT = Engine_Api::_()->getDbtable('promotions','talktangle');
		$select  = $promotionT ->select()
							  -> where('promotion_id in (?) = ?',$ids); 	
        
        
        $this->view->paginator = $paginator = Zend_Paginator::factory($select);

        // Set item count per page and current page number
        $paginator->setItemCountPerPage($this->_getParam('itemCountPerPage', 4));
        $paginator->setCurrentPageNumber($this->_getParam('page', 1));
        $this->view->paginator = $paginator;
     
       
	}
}